/******************************************************************************
* File description: IndexAnalysisDataTable.qml
* Function: 主页分析数据表格
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-20
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import Qt.labs.qmlmodels 1.0

Item {
    id: root

    /* property 定义开始 */
    // 表格数据
    property var tableModels: []

    // 私有属性
    property var _horHeaderData: ["序号", "孔位", "样本名称", "样本类型", "荧光染料", "CT值",
        "样本浓度\nng/μl", "总浓度\nng/μl", "男性浓度\nng/μl", "女性浓度\nng/μl",
        "性别情况", "男女浓度\n比例", "降解指数\nDI", "抑制指数\nICS"];
    property int _horHeaderHeight: 55
    property int _tableItemHeight: 40
    property var _tableModel: tableModels

    // 函数
    // 重置表格位置
    function resetTableViewPosition(posX, posY){
        if(posX !== undefined && posX !== null){
            tableContent.contentX = posX
        }
        if(posY !== undefined && posY !== null){
            tableContent.contentY = posY
        }
    }
    /* property 定义结束 */

    Item {
        id: container
        anchors.fill: parent
        anchors.topMargin: 10

        // 表头部分
        // 横向表头
        Item {
            id: horHeaderFlickable
            width: parent.width
            height: _horHeaderHeight

            Item {
                id: horizontalHeader
                anchors.fill: parent

                Row {
                    id: horHeaderRowLayout
                    spacing: 0

                    Repeater {
                        id: horHeaderItemRepeater
                        model: _horHeaderData

                        Item {
                            id: horheaderItem
                            width: horizontalHeader.width / _horHeaderData.length
                            implicitHeight: horizontalHeader.height

                            Rectangle {
                                id: horHeaderBg
                                anchors.fill: parent
                                border.width: 0
                                color: "transparent"
                                clip: true

                                Text {
                                    id: horHeaderText
                                    text: qsTr(modelData)
                                    anchors.centerIn: parent
                                    font.pixelSize: 14
                                    font.family: "Alibaba PuHuiTi"
                                    color: "#FF222222"
                                    leftPadding: 15
                                    rightPadding: 15
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    wrapMode: Text.WordWrap
                                }
                            }

                            // 画边框
                            Canvas {
                                id: horHeaderCanvas
                                antialiasing: true
                                anchors.fill: parent
                                onPaint: {
                                    var ctx = getContext("2d")
                                    ctx.clearRect(0, 0, width, height)
                                    ctx.strokeStyle = "#FF999999"
                                    ctx.lineWidth = 2
                                    ctx.beginPath()

                                    // 上边框
                                    ctx.moveTo(0, 0)
                                    ctx.lineTo(width, 0)
                                    ctx.stroke()

                                    // 下边框
                                    ctx.moveTo(0, height)
                                    ctx.lineTo(width, height)
                                    ctx.stroke()
                                }
                            }

                            // 左边框
                            Canvas {
                                id: horHeaderLeftBorder
                                visible: index !== 0
                                antialiasing: true
                                anchors.fill: parent
                                onPaint: {
                                    // 左边框
                                    var ctx = getContext("2d")
                                    ctx.clearRect(0, 0, width, height)
                                    ctx.strokeStyle = "#FF999999"
                                    ctx.lineWidth = 2
                                    ctx.beginPath()
                                    ctx.moveTo(0, 0)
                                    ctx.lineTo(0, height)
                                    ctx.stroke()
                                }
                            }
                        }
                    }
                }
            }
        }

        // 表格部分
        TableView {
            id: tableContent
            height: parent.height - horHeaderFlickable.height
            anchors {
                left: parent.left
                right: parent.right
                top: parent.top
                topMargin: _horHeaderHeight
            }
            clip: true
            boundsBehavior: Flickable.StopAtBounds
            columnSpacing: 0
            rowSpacing: 0

            columnWidthProvider: function(column){
                return container.width / _horHeaderData.length
            }

            rowHeightProvider: function(row){
                return _tableItemHeight
            }

            model: TableModel {
                id: tableModel

                TableModelColumn {display: "序号"}
                TableModelColumn {display: "孔位"}
                TableModelColumn {display: "样本名称"}
                TableModelColumn {display: "样本类型"}
                TableModelColumn {display: "荧光染料"}
                TableModelColumn {display: "CT值"}
                TableModelColumn {display: "样本浓度ng/μl"}
                TableModelColumn {display: "总浓度ng/μl"}
                TableModelColumn {display: "男性浓度ng/μl"}
                TableModelColumn {display: "女性浓度ng/μl"}
                TableModelColumn {display: "性别情况"}
                TableModelColumn {display: "男女浓度比例"}
                TableModelColumn {display: "降解指数DI"}
                TableModelColumn {display: "抑制指数ICS"}

                rows: _tableModel
            }

            delegate: Rectangle {
                id: tableDelegate
                color: "transparent"
                width: tableContent.columnWidthProvider(column)
                height: tableContent.rowHeightProvider(0)
                implicitWidth: tableItem.width
                border.width: 0
                clip: true

                Label {
                    id: tableItem
                    anchors.fill: parent
                    font.family: "Alibaba PuHuiTi"
                    font.pixelSize: 14
                    color: "#FF222222"
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    text: qsTr(display && (typeof display === "string") ? display : "")
                    antialiasing: true
                    clip: true
                    elide: Text.ElideRight

                    // 文本过长，显示ToolTips
                    Label {
                        id: innerTableItem
                        visible: false
                        text: parent.text
                    }

                    ToolTip {
                        id: tooltip
                        visible: innerTableItem.width > tableItem.width &&
                                 itemDeleMouseArea.containsMouse
                        x: 0
                        y: tableDelegate.height
                        z: 999
                        text: parent.text
                        font: parent.font
                    }
                }

                // 边框
                Canvas {
                    anchors.fill: parent
                    antialiasing: true
                    onPaint: {
                        var ctx = getContext("2d")
                        ctx.strokeStyle = "#FF999999"
                        ctx.lineWidth = 2
                        // 下边框
                        ctx.moveTo(0, height)
                        ctx.lineTo(width, height)
                        ctx.stroke()
                    }
                }

                // 左边框
                Canvas {
                    visible: column !== 0
                    antialiasing: true
                    anchors.fill: parent
                    onPaint: {
                        // 左边框
                        var ctx = getContext("2d")
                        ctx.strokeStyle = "#FF999999"
                        ctx.lineWidth = 2
                        ctx.moveTo(0, 0)
                        ctx.lineTo(0, height)
                        ctx.stroke()
                    }
                }

                // 鼠标事件
                MouseArea {
                    id: itemDeleMouseArea
                    anchors.fill: parent
                    hoverEnabled: true
                }
            }

            // 事件
            onHeightChanged: {
                forceLayout() // 强制重新布局刷新视图
            }
        }
    }
}
